package com.bianxianmao.myinfo.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.bianxianmao.R;

public class CustomDialog extends Dialog {

    public CustomDialog(Context context, int theme) {
        super(context, theme);

    }

    public CustomDialog(Context context) {
        this(context, R.style.dialog_anim_style);
    }

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {

        private Context context;
        private boolean isMultiChoice = false;
        private boolean titleShowCenter = false;// 标题是否显示在中间

        public final static int BOTTOME_TYPE_DIALOG = 0;
        public final static int CENTER_TYPE_DIALOG = 1;
        private int style = BOTTOME_TYPE_DIALOG;

        private OnClickListener titleRightButtonClickListener;
        private OnClickListener titleLeftButtonClickListener;

        private Button btnDialogLeft;// 左边取消
        private Button btnDialogRight;// 右边OK
        private LinearLayout cententLayout;

        private View contentView;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setOkListener(OnClickListener listener) {
            this.titleRightButtonClickListener = listener;
            return this;
        }

//		public Builder setCancelListener(OnClickListener listener) {
//			this.titleLeftButtonClickListener = listener;
//			return this;
//		}

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Create the custom dialog
         */
        @SuppressLint("Override")
        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.dialog);
            View layout = inflater.inflate(R.layout.layout_custom_dialog, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            int width = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
            Window window = dialog.getWindow();

            window.setGravity(Gravity.BOTTOM);
            window.setLayout(width, LayoutParams.WRAP_CONTENT);
            window.setWindowAnimations(R.style.dialog_anim_style2);
            window.setBackgroundDrawableResource(R.color.transparent);

            findView(layout);

            if (titleRightButtonClickListener != null) {

                btnDialogRight.setVisibility(View.VISIBLE);
                btnDialogRight.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        titleRightButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
                    }
                });
            } else {
                btnDialogRight.setVisibility(View.GONE);
            }

//			if (titleLeftButtonClickListener != null) {
//				btnDialogLeft.setOnClickListener(new View.OnClickListener() {
//					public void onClick(View v) {
//						titleLeftButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEUTRAL);
//					}
//				});
//			} else {
//				btnDialogLeft.setVisibility(View.GONE);
//			}
            if (contentView != null) {
                cententLayout.removeAllViews();
                cententLayout.addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT, // WRAP_CONTENT
                        LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }

        private void findView(View layout) {
//			btnDialogLeft = (Button) layout.findViewById(R.id.btnDialogLeft);
            btnDialogRight = (Button) layout.findViewById(R.id.btnDialogRight);
            cententLayout = (LinearLayout) layout.findViewById(R.id.dialog_content_layout);
        }
    }


}